#include "yasaigraphicsview.h"

#include "math.h"
#include <QDebug>

YasaiGraphicsView::YasaiGraphicsView(QWidget *parent)
    :
    QGraphicsView(parent),
    mZoomAdjustment(0)
{
    this->installEventFilter(this);
    this->setMouseTracking(true);
    this->setAcceptDrops(true);
    msUntilHideCursor = 0;
    mTimer = new QTimer(this);
    QObject::connect(mTimer, &QTimer::timeout, this, &YasaiGraphicsView::updateCursor);
    mTimer->start(1000);
}

YasaiGraphicsView::YasaiGraphicsView(QGraphicsScene *scene, QWidget *parent)
    :
    QGraphicsView(scene, parent),
    mZoomAdjustment(0)
{
    this->installEventFilter(this);
    this->setMouseTracking(true);
    this->setAcceptDrops(true);
    msUntilHideCursor = 0;
    mTimer = new QTimer(this);
    QObject::connect(mTimer, &QTimer::timeout, this, &YasaiGraphicsView::updateCursor);
    mTimer->start(1000);
}

YasaiGraphicsView::~YasaiGraphicsView()
{
    delete mTimer;
}

void YasaiGraphicsView::setHideCursor(bool hide)
{
    msUntilHideCursor = 0;
    if (hide) mTimer->start();
    else mTimer->stop();
}

bool YasaiGraphicsView::eventFilter(QObject *obj, QEvent *event)
{
    if (obj != this) return false;
    if (event->type() != QEvent::KeyPress) return false;
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    if (keyEvent->key() == Qt::Key_PageUp) return true;
    if (keyEvent->key() == Qt::Key_PageDown) return true;
    return false;
}

void YasaiGraphicsView::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu(this);
    menu.addActions(this->actions());
    menu.exec(event->globalPos());
}

void YasaiGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    if (!mTimer->isActive()) mTimer->start();
    if (this->cursor().shape() == Qt::BlankCursor) this->setCursor(QCursor(Qt::ArrowCursor));
    msUntilHideCursor = 0;
    QGraphicsView::mouseMoveEvent(event);
}

void YasaiGraphicsView::updateCursor()
{
    if (msUntilHideCursor >= this->HIDETIME)
    {
        this->setCursor(QCursor(Qt::BlankCursor));
        mTimer->stop();
        return;
    }
    this->msUntilHideCursor++;
}

void YasaiGraphicsView::mouseDoubleClickEvent(QMouseEvent *)
{
    emit this->activateFullScreen();
}

void YasaiGraphicsView::dragEnterEvent(QDragEnterEvent *event)
{
    if(!event->mimeData()->hasUrls()) return;
    event->setDropAction(Qt::LinkAction);
    event->accept();
}

void YasaiGraphicsView::dragMoveEvent(QDragMoveEvent *event)
{
    if(!event->mimeData()->hasUrls()) return;
    event->setDropAction(Qt::LinkAction);
    event->accept();
}

void YasaiGraphicsView::dropEvent(QDropEvent* event)
{
    const QMimeData* mimeData = event->mimeData();
    if (!mimeData->hasUrls()) return;
    QList<QUrl> urls = mimeData->urls();
    emit openDroppedFile(QFileInfo(urls.at(0).toLocalFile()), QString());
}

void YasaiGraphicsView::wheelEvent(QWheelEvent *event)
{
    if (event->modifiers().testFlag(Qt::ControlModifier))
    {
        mZoomAdjustment += (event->delta() / 120)*10;
        emit changed();
    }
    else
    {
        QGraphicsView::wheelEvent(event);
    }
}

int YasaiGraphicsView::getZoomAdjustment() const
{
    return mZoomAdjustment;
}
